package com.dvd.common;
import java.util.ArrayList;
import java.util.List;

import com.dvd.cjc.gcjc001.GCJC001Model;
import com.dvd.dao.usr.*;
import com.dvd.utils.Message;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class CommonAction extends Action {

	
	public void searchAdminNm(String id)
	{
		CommonModel model = new CommonModel();
		model.searchAdminNm(id);
	}
	
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CommonAction.New(request,this.servlet);
		String action =request.getParameter("action");
		if(action.equals("toEditAdmin"))
		{
			return this.toEditAdmin(mapping, form, request, response);
		}
		else if(action.equals("toLogin"))
		{
			return this.toLogin(mapping, form, request, response);
		}
		else if(action.equals("logout"))
		{
			
			return this.logout(mapping, form, request, response);
		}
		else if(action.equals("toDvdList"))
		{
			return this.toDvdList(mapping, form, request, response);
		}
		else if(action.equals("toAddDvd"))
		{
			return this.toAddDvd(mapping, form, request, response);
		}
		else if(action.equals("toSearch"))
		{
			return this.toSearch(mapping, form, request, response);
		}
		else
		{
			assert("INVALID　ACTION" ==null);
			return null;
		}
	}

	public ActionForward toEditAdmin(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		String loginId = request.getParameter("loginId");
		if(loginId == null)
		{
			return mapping.findForward("GCJC001");
		}
		GCJC001Model model = new GCJC001Model();
		TUsr usr = model.searchById(loginId);
		request.setAttribute("TUsr", usr);
		return mapping.findForward("GCJC001");
		//return new ActionForward("/GCJC001.do?loginId=" + request.getParameter("loginId"));
	}
	public ActionForward toLogin(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		return mapping.findForward("GLIA001");
	}
	
	public ActionForward logout(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		assert(request.getSession().getAttribute("loginState")!=null);
		
		request.getSession().setAttribute("loginState",null);
		request.getSession().setAttribute("loginId",null);
		List<Message> messages =  new ArrayList<Message>();
		messages.add(new Message("","INFO","管理者情報は成功にログアウトしました！"));
		request.setAttribute("List<Message>",messages);
		return new ActionForward("/GDJK001.do?action=paging");
	}
	
	public ActionForward toDvdList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		return new ActionForward("/GDJK001.do?action=paging");
	}
	public ActionForward toAddDvd(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		return new ActionForward("/GDJC002.do?action=new");
	}
	
	public ActionForward toSearch(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	{
		return mapping.findForward("GDJK002");
	}
	
	public static void New(HttpServletRequest request, HttpServlet servlet)
	{
		CommonModel model =new CommonModel();
		TUsr usr=model.searchAdminNm();
		if(usr==null)
		{
			servlet.getServletContext().setAttribute("registerState", null);
			request.getSession().setAttribute("loginState",null);
			request.getSession().setAttribute("loginId", null);
		}
		else
		{
			servlet.getServletContext().setAttribute("registerState", "true");
			if(request.getSession().getAttribute("loginState")!=null)
			{
				request.getSession().setAttribute("loginState", usr.getFUsrNm());
				request.getSession().setAttribute("loginId", usr.getFLoginId());
			}
		}
	}
	
	
}
